package com.tjyx.maintain.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tjyx.common.action.BaseAction;
import com.tjyx.maintain.dao.NewsClassDao;
import com.tjyx.maintain.dto.NewsClass;

public class NewsClassAction extends BaseAction{

	
	public String addNewsClass(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
	 String newsClassName=request.getParameter("newsClassName");
	 NewsClassDao newsClassDao=new  NewsClassDao();
	 if(newsClassName!=null&&!newsClassName.equals("")){
		 System.out.print("newsClassName=="+newsClassName);
		 try {
			newsClassDao.addNewsClass(newsClassName);
			request.setAttribute("addMessage","添加成功！");
		} catch (SQLException e) {
			request.setAttribute("addMessage","添加失败！");
			e.printStackTrace();
		}
	 }
		 try {
			List<NewsClass> allNewsClass=newsClassDao.queryNewsClass();
			request.setAttribute("allNewsClass",allNewsClass);
			request.setAttribute("queryMessage","查询成功！");
		} catch (SQLException e) {
			request.setAttribute("queryMessage","查询失败！");
			e.printStackTrace();
		}
	return "maintain/news/buildUpNewsClass.jsp";
	}
	

	public String deleteNewsClass(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		 NewsClassDao newsClassDao=new  NewsClassDao();
		 String id=request.getParameter("id");
		 System.out.println("id===="+id);
		 int idDelete=0;
		 if(id!=null&&!id.equals("")){
			 idDelete=Integer.valueOf(id);
		 }
		 try {
			newsClassDao.deleteNewsClass(idDelete);
			request.setAttribute("delete","删除成功！");
		} catch (SQLException e) {
			request.setAttribute("delete","删除失败！");
			e.printStackTrace();
		}
		PrintWriter pw=response.getWriter();
		pw.print(request.getAttribute("delete").toString());
		return "NoResult";
	}
	
	public String updateNewsClass(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String id=request.getParameter("id");
		String className=request.getParameter("classname");
		className=new   String(className.getBytes("iso-8859-1"),"utf-8");
		System.out.println("id classname=="+id+"  "+className);
		 NewsClassDao newsClassDao=new  NewsClassDao();
		 try {
			newsClassDao.updateNewsClass(Integer.valueOf(id), className);
			request.setAttribute("update","修改成功！");
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			request.setAttribute("update","修改失败！");
			e.printStackTrace();
		}
		PrintWriter pw=response.getWriter();
		pw.print(request.getAttribute("update").toString());
		return "NoResult";
	}
}
